const http = require('http');
const {pingConfig} = require("../../notifier.config");

function pingInterface(lastIp, port, name) {
    var options = {
        hostname: `192.168.1.${lastIp}`,
        port,
        path: '/api/upms/common/global/config',
        method: 'GET',
    };
    return new Promise(((resolve) => {
        var req = http.request(options, (res) => {
            resolve(`<div style="background-color: coral;">${name}---${options.hostname}:${options.port}--服务已启动</div>`);
        });

        req.on('error', (e) => {
            resolve(`<div>${name}---${options.hostname}:${options.port}--XXXXXXXX</div>`)
        });
        req.end();
    }))
}

function pingAll(cb) {
    let count = 0;
    return new Promise(resolve => {
        for (const lastIpElement of pingConfig.ip) {
            pingInterface(...lastIpElement).then(res => {
                cb(res);
                count++;
                if(count === pingConfig.ip.length) {
                    resolve();
                }
            })
        }
    })
}

module.exports = {
    pingAll,
}
